<?php

/**
 * This controller do the functions like: show list, deleted, redirect paypal
 */
class ProductController extends Controller {

    /**
     * show list product include both product and custom case
     * @param int $id is category id
     */
    public function actionList_product($id) {

	$criteria = new CDbCriteria();

	if ($id == CUSTOM_CASE_CAT) {
	    $model = new CustomPhone;
	} else {
	    $model = new Product;
	    $criteria->addCondition('t.category_id =' . $id);
	}
	$criteria->order = 'update_time DESC';
	$iTotalRecord = $model->count($criteria);

	$pages = new CPagination($iTotalRecord);
	$pages->pageSize = Product::TOTAL_RECORD;
	$pages->applyLimit($criteria);

	$aCat = Category::getListCat();

	//get id of category
	foreach ($aCat as $key => $cat) {
	    if ($cat['category_id'] == $id) {
		$idArr = $key;
	    }
	}

	$this->render('list_product', array(
	    'model' => $model,
	    'aProduct' => $model->findAll($criteria),
	    'pages' => $pages,
	    'iTotalRecord' => $iTotalRecord,
	    'aCat' => $aCat,
	    'id' => $id,
	    'idArr' => $idArr
	));
    }

    /**
     * Show detail of product
     * @param int $id is product id
     * @throws CHttpException
     */
    public function actionDetail($id) {
	$model = Product::model()->findByPk($id);
	if ($model) {

	    if (isset($_POST['submit'])) {

		$totalPurchase = floatval($model->price) * intval($_POST['total_order']);
		if ($model->sale) {
		    $sale = $totalPurchase * floatval(floatval($model->sale) / 100);
		    $totalPurchase = $totalPurchase - $sale;
		}

		//save the ordering information
		$order = array(
		    'model_id' => OrderDetail::MODEL_PRODUCT,
		    'name' => $model->name,
		    'total_order' => $_POST['total_order'],
		    'total_purchase' => $totalPurchase,
		    'product_id' => $id,
		    'image_product' => $model->image,
		);
		if (isset(Yii::app()->session['order'])) {
		    $aOrder = Yii::app()->session['order'];
		    array_push($aOrder, $order);
		    Yii::app()->session['order'] = $aOrder;
		} else {
		    Yii::app()->session['order'] = array(0 => $order);
		}

		//add to cart	    
		$this->redirect(url('cart'));
	    }

	    $aCat = Category::getListCat();
	    //get id of category
	    foreach ($aCat as $key => $cat) {
		if ($cat['category_id'] == $id) {
		    $idArr = $key;
		}
	    }

	    //get relation product
	    $relateProduct = Product::model()->findAll(array(
		'condition' => 'category_id=' . $model->category_id . ' and product_id!=' . $id,
		'order' => 'rand()',
		'limit' => 4
	    ));

	    $this->render('detail', array(
		'model' => $model,
		'aCat' => $aCat,
		'idArr' => $idArr,
		'relateProduct' => $relateProduct));
	} else {
	    throw new CHttpException(400, 'The page is not found');
	}
    }

    public function actionPdf() {

//	require ROOT . '/common/extensions/MPDF54/mpdf.php';
//
//	Yii::import('common.extensions.MPDF54.mpdf');
//
//	$html = '<div style="width: 490px;">
//	    <div style="position: relative;overflow: hidden;width: 490px;height: 600px;">
//		<img style="z-index: 1;top: 215px;position: absolute;width: 103px;left: 192px;" src="http://localhost/yii/phonecase/www/jquery_file_upload/server/php/files/close-icon.png">
//		<img style="z-index: 2;position: absolute;left: -61px;" src="http://localhost/yii/phonecase/www/uploads/customcase/1386054176.png">		
//	    </div>
//	</div>';
//
//
//	$mpdf = new mPDF();
//	$mpdf->SetAutoFont(AUTOFONT_ALL);
//	$mpdf->WriteHTML($html);
//	$mpdf->Output('DKHP.pdf', 'D'); //xuat ra file pdf voi thao tac down load ve
//
//	exit;
    }

}

